package com.org;

import java.io.File;
import java.io.FileInputStream;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;
import java.util.Properties;

import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

import org.apache.log4j.PropertyConfigurator;

import com.org.utils.conf.ConfigureFactory;
import com.org.utils.conf.ConfigureMacro;
import com.org.utils.io.SearchFileUtil;

/**
 *初始化系统所有配置文件
 */
public class ParameterInit implements ServletContextListener {

	public void contextDestroyed(ServletContextEvent event) {

	}


	/**
	 * 1. 加载home配置文件 2. 读取home路径，将HOME变量存在在宏中 3.
	 * 读取home目录下所有的文件，加载所有的properties文件和xml文件，分别读取properties和xml文件，
	 * 当properties中有HOME的相对路径时，替换为绝对路径
	 */
	public void contextInitialized(ServletContextEvent event) {
		ServletContext sc = event.getServletContext();
		String etcHomeFilepath = sc.getInitParameter("InitConfigLocation");
		String className = this.getClass().getName();
		sc.log(className + "' init starting");
		try {
			sc.log("Servlet Context info: " + sc.getServerInfo());
			sc.log("Servlet version: " + sc.getMajorVersion() + "."
					+ sc.getMinorVersion());
			sc.log("Servlet Context real root: " + sc.getRealPath("/"));

			String etcHome = null;
			String webHome = null;
			if (etcHomeFilepath != null) {
				try {
					etcHome = ConfigureFactory.getDefaultPropertes("etc_home",
							etcHomeFilepath);
					webHome = ConfigureFactory.getDefaultPropertes(
							"dress_home", etcHomeFilepath);
				} catch (Exception e) {
				}
			} else {
				try {
					etcHome = ConfigureFactory.getDefaultPropertes("etc_home");
					webHome = ConfigureFactory
							.getDefaultPropertes("dress_home");
				} catch (Exception e) {
				}
			}
			ConfigureMacro.addMacro("etc_home", etcHome);

			List<String> ectHomes = new ArrayList<String>(2);
			if (etcHome != null && !etcHome.equals("")) {
				ectHomes.add(etcHome);
			}
			if (webHome != null && !webHome.equals("")) {
				ectHomes.add(webHome);
			}
			for (String home : ectHomes) {
				SearchFileUtil sf = new SearchFileUtil(home, ".*\\.properties");
				List<File> confFile = sf.getCurrentFiles();
				int size = confFile.size();
				for (int i = 0; i < size; i++) {
					Properties p = new Properties();
					FileInputStream fis = null;
					Enumeration<?> en = null;
					File f = confFile.get(i);
					fis = new FileInputStream(f);
					p.load(fis);
					if (f.getName().equals("log4j.properties")) {
						p.setProperty("etc_home", etcHome);
						PropertyConfigurator.configure(p);
						sc.log(this.getClass() + " initialize log4j success");
					} else {
						en = p.propertyNames();
						while (en.hasMoreElements()) {
							String key = (String) en.nextElement();
							String value = p.getProperty(key);
							if (value.contains("${etc_home}")) {
								value = value.replace("${etc_home}", etcHome);
							}
							ConfigureMacro.addMacro(key, value);
							sc.setAttribute(key, value);
							fis.close();
						}
					}
				}
			}
			sc.log(className + "' init finished");
		} catch (Exception e) {
			e.printStackTrace();
			sc.log(className + "Init Exception:" + e.getMessage());
		}

	}

}
